using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace Imagine
{
    public class Engine
    {
        #region Engine.Properties
        public SpriteBatch SpriteBatch
        {
            get { return m_spriteBatch;   }
            set { m_spriteBatch = value;  }
        }

        public int ScreenHeight
        {
            get { return m_instance.GraphicsDeviceManager.PreferredBackBufferHeight; }
        }

        public int ScreenWidth
        {
            get { return m_instance.GraphicsDeviceManager.PreferredBackBufferWidth;  }
        }

        public SpriteFont SpriteFont
        {
            get { return m_font;   }
            set { m_font = value;  }
        }

        public Camera Camera
        {
            get { return m_camera;   }
            set { m_camera = value;  }
        }

        public GraphicsDeviceManager GraphicsDeviceManager
        {
            get { return m_graphics;  }
        }

        public GraphicsDevice GraphicsDevice
        {
            get { return m_graphics.GraphicsDevice;  }
        }

        public ContentManager Content
        {
            get { return m_content;  }
            set { m_content = value; }
        }
        #endregion

        public void Ignite(GraphicsDeviceManager graphics, ContentManager content, int MaxRenderlayers, Vector2 gravity, bool sleep)
        {
            m_spriteBatch   = null;
            m_graphics      = graphics;
            m_content       = content;
            m_camera        = new Camera(m_graphics.GraphicsDevice);
            m_font          = null;

            TheWorld.Instance().Initialize(graphics, MaxRenderlayers, gravity, sleep);
            //for Resolution Independent Rendering 
            Resolution.SetVirtualResolution(graphics.PreferredBackBufferWidth, graphics.PreferredBackBufferHeight);
            Resolution.Init(ref graphics);
        }
        public void Release()
        {}
        
        public void Update(GameTime gameTime)
        {
            //Update your Screen manager, Camera, input etc here
            TheWorld.Instance().Update(gameTime);
            TheScreenManager.Instance().Update(gameTime);
            TheInputManager.Instance().Update();
        }

        public void Draw(GameTime gameTime)
        {
            Resolution.BeginDraw();
            TheScreenManager.Instance().Draw(gameTime, SpriteBatch);
            TheWorld.Instance().Draw(gameTime);
        }

        static public Engine Instance()
        {
            if(m_instance == null)
                m_instance = new Engine();
            return m_instance;
        }
        
        #region Private_Parts
        static private Engine   m_instance;
        GraphicsDeviceManager   m_graphics;
        ContentManager          m_content;
        SpriteBatch             m_spriteBatch;
        SpriteFont              m_font;
        Camera                  m_camera;
        #endregion

    }
}
